package com.lvyq.qrobot;

import com.alibaba.fastjson2.JSONObject;
import com.lvyq.qrobot.util.HttpClientUtil;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * @author lvyq
 * @version 1.0
 * Create by 2024-07-11 16:15
 */
public class Demo5 {
    public static void main(String[] args) throws IOException {
        String result = "";
        TcGpt tcGpt = new TcGpt();
        tcGpt.setStream(false);
        tcGpt.setPrompt("讲个笑话");
        String url ="https://chat18.aichatos.xyz/api/generateStream?refer__1360=CqRhGKDIqjgKDsoYYIxiInGx0OQlAbeD";
        String params = JSONObject.toJSONString(tcGpt);
        System.out.println("s = " +params);
         String res = sendPost(url,params );

        JSONObject mav =JSONObject.parseObject(res);
        result=mav.getString("data");
        System.out.println("result = " + result);


    }




    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("content-type","application/json");
            conn.setRequestProperty("Accept","application/json, text/plain, */*");
            conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
            conn.setRequestProperty("Accept-Language","zh-CN");
            conn.setRequestProperty("Referer","https://chat18.aichatos.xyz/");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
            out.print(param);
            out.flush();

            String line;
            for(in = new BufferedReader(new InputStreamReader(conn.getInputStream())); (line = in.readLine()) != null; result = result + line) {
            }
        } catch (Exception var16) {
            System.out.println("发送 POST 请求出现异常！" + var16);
            var16.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }

                if (in != null) {
                    in.close();
                }
            } catch (IOException var15) {
                var15.printStackTrace();
            }

        }

        return result;
    }


    public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        String token = "ZXlKMGVYQWlPaUpLVjFRaUxDSmhiR2NpT2lKSVV6STFOaUo5LmV5SjBkQ0k2TkN3aVlYVmtJam9pTkRVeU5USXdaR1pqTWpGbU5ETXpOVGxoTlRaa09UWXlPRFk0TkRsa01UTWlMQ0pzZFNJNklteDJlWEVpTENKbGVIQWlPakl4TkRVNE9EZ3dNREFzSW5WeUlqb3lMQ0pxZEdraU9pSkJVRWxmVkU5TFJVNWZORFV5TlRJd1pHWmpNakZtTkRNek5UbGhOVFprT1RZeU9EWTRORGxrTVRNdE5DSjkubjA4T18zalNYZ080eDF6UWFrSE9yM0d1dEYxOFp4akkxTU1YVHhSN19IWQ==";

        try {
            String urlNameString = url;
            java.net.URL realUrl = new URL(urlNameString);
            URLConnection connection = realUrl.openConnection();
            connection.setRequestProperty("Authorization","Bearer "+token);
            connection.setRequestProperty("Accept","*/*");
            connection.connect();
            Map<String, List<String>> map = connection.getHeaderFields();
            Iterator var8 = map.keySet().iterator();

            while(var8.hasNext()) {
                String key = (String)var8.next();
                //  System.out.println(key + "--->" + map.get(key));
            }

            String line;
            for(in = new BufferedReader(new InputStreamReader(connection.getInputStream())); (line = in.readLine()) != null; result = result + line) {
            }
        } catch (Exception var18) {
            System.out.println("发送GET请求出现异常！" + var18);
            var18.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception var17) {
                var17.printStackTrace();
            }

        }

        return result;
    }

    private static void JsonType() throws IOException {
        String token = "ZXlKMGVYQWlPaUpLVjFRaUxDSmhiR2NpT2lKSVV6STFOaUo5LmV5SjBkQ0k2TkN3aVlYVmtJam9pTkRVeU5USXdaR1pqTWpGbU5ETXpOVGxoTlRaa09UWXlPRFk0TkRsa01UTWlMQ0pzZFNJNklteDJlWEVpTENKbGVIQWlPakl4TkRVNE9EZ3dNREFzSW5WeUlqb3lMQ0pxZEdraU9pSkJVRWxmVkU5TFJVNWZORFV5TlRJd1pHWmpNakZtTkRNek5UbGhOVFprT1RZeU9EWTRORGxrTVRNdE5DSjkubjA4T18zalNYZ080eDF6UWFrSE9yM0d1dEYxOFp4akkxTU1YVHhSN19IWQ==";
        URL url = new URL("https://api.devbit.cn/ai/api/ai/chat");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Bearer " + token);
        conn.setRequestProperty("Accept", "*/*");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);
        OutputStream os = conn.getOutputStream();
        String requestBody = "{\"prompt\":\"请扮演一个解决方案专家，无论我提出什么问题，请直接给出方案，不要有其他多余的文字\",\"question\":\"怎么把 5 个苹果合理地分给 6 个小朋友\",\"stream\":false}";
        os.write(requestBody.getBytes());
        os.flush();
        os.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
    }
}
